﻿using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using metroioc.Container;
using metroioc.tests.Artefacts;

namespace MetroIoc.Tests
{
    [TestClass]
    public class ContainerTests
    {
        [TestMethod]
        public void BuildUpAppliesDependencyProperty()
        {
            var container = new MetroContainer();

            container.GetConfiguration()
                .Property<TestClassWithProperty, string>(x => x.CustomerName, "TestCustomerName");

            var obj = new TestClassWithProperty();

            container.BuildUp(obj);

            Assert.AreEqual("TestCustomerName", obj.CustomerName);
        }

        [TestMethod]
        public void BuildUpDoesNotApplyStandardProperty()
        {
            var container = new MetroContainer();

            container.GetConfiguration()
                .Property<TestClassWithProperty, string>(x => x.CustomerName, "TestCustomerName")
                .Property<TestClassWithProperty, string>(x => x.NotInjected, "NotInjected");

            var obj = new TestClassWithProperty();

            container.BuildUp(obj);

            Assert.IsNull(obj.NotInjected, string.Format("Should have been null, but was {0}", obj.NotInjected));
        }

        [TestMethod]
        public void BuildUpResolvesAllInjectedProperties()
        {
            var container = new MetroContainer()
                .Register<IFirst, First>()
                .Register<ISecond, Second>();

            container.GetConfiguration()
                .Property<TestClassWithProperties, string>(x => x.CustomerName, "TestCustomerName");

            var obj = new TestClassWithProperties();

            container.BuildUp(obj);

            Assert.AreEqual("TestCustomerName", obj.CustomerName);

            Assert.IsNotNull(obj.FirstProperty, "FirstProperty shouldn't be null");
            Assert.IsInstanceOfType(obj.FirstProperty, typeof(First), "FirstProperty should a 'First' object");

            Assert.IsNotNull(obj.SecondProperty, "SecondProperty shouldn't be null");
            Assert.IsInstanceOfType(obj.SecondProperty, typeof(Second), "SecondProperty should a 'Second' object");
        }
    }
}
